package com.andnnl.fileanalysis;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenss on 2021/6/2.
 */
public abstract class AbsFileAnalysis implements FileAnalysis {
    private List<List<String>> list = new ArrayList<>();
    private List<Integer> rowNumList = new ArrayList<>();

    public AbsFileAnalysis() {

    }

    @Override
    public abstract void analysis(InputStream is, AnalysisListenerBase listener, AnalysisParam param);

    @Override
    public void handleRow(List<String> row,Integer rowNum, AnalysisListenerBase listener, AnalysisParam param) {
        if (param.getBatchSize() > 1) {
            list.add(row);
            rowNumList.add(rowNum);
            if (list.size() == param.getBatchSize()) {
                listener.invokeBatch(list,rowNumList);
                list.clear();
                rowNumList.clear();
            }
        } else {
            listener.invoke(row,rowNum);
        }
    }

    /**
     * 读取完成之后调用该方法，如果是最后一批
     *
     * @param listener
     * @param param
     */
    @Override
    public void finish(AnalysisListenerBase listener, AnalysisParam param) {
        if (param.getBatchSize() > 1 && list.size() > 0) {
            listener.invokeBatch(list,rowNumList);
            list.clear();
            rowNumList.clear();
        }
    }


}
